using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace BTSProjectWizard.Forms
{
    public partial class FilterExpressionPage : BTSProjectWizard.BTSWizardPage
    {
        public FilterExpressionPage()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void FilterExpressionPage_Load(object sender, EventArgs e)
        {
            //add the properties
            object props = wizard.FindSymbol("PropertiesPicked");
            if (props != null)
            {
                string[] theprops = props.ToString().Split('|');
                foreach (string p in theprops)
                {
                    Property.Items.Add(p);
                }
            }
        }
        public override bool ProcessSymbols()
        {
            bool ret = true;
            MemoryStream ms = new MemoryStream();
            XmlTextWriter tw = new XmlTextWriter(ms, System.Text.Encoding.Default);
            //wizard.Add("dummyxml","<s:doc xmlns:s='http://Schema1'></s:doc>");
            tw.WriteStartDocument();
            tw.WriteStartElement("doc");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value != null)
                {
                    tw.WriteStartElement("ctypeproperty");
                    tw.WriteElementString("id", Guid.NewGuid().ToString());
                    tw.WriteElementString("lhs", dataGridView1.Rows[i].Cells[0].Value.ToString());
                    tw.WriteElementString("rhs", dataGridView1.Rows[i].Cells[2].Value==null?"":dataGridView1.Rows[i].Cells[2].Value.ToString());
                    tw.WriteElementString("grouping", dataGridView1.Rows[i].Cells[3].Value==null?"AND":dataGridView1.Rows[i].Cells[3].Value.ToString());
                    tw.WriteElementString("operator", dataGridView1.Rows[i].Cells[1].Value.ToString());
                    tw.WriteEndElement();//var
                }
                
            }

            tw.WriteEndElement();
            tw.WriteEndDocument();
            tw.Flush();
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(ms);
            string xml = sr.ReadToEnd();
            wizard.AddSymbol("dummyxml", xml);
            sr.Close(); tw.Close(); ms.Close();
            return ret;
        }
    }
}

